package com.szt.sy.controller.system;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.szt.sy.config.ApplicationConfig;
import com.szt.sy.entity.system.ImageType;
import com.szt.sy.service.system.imagetype.ImageTypeService;
import com.szt.sy.service.system.systemimage.SystemImageService;
import com.szt.sy.util.img.ImgUtils;
import com.szt.sy.util.web.FileUpload;
import com.szt.sy.vo.rep.base.RepBaseVO;

/***
 * 文件上传-controller
 * 
 * @author wwy
 *
 */
@Controller
@RequestMapping("admin/file")
public class AdminFileController {
	
	@Autowired
	private ApplicationConfig applicationConfig;
	
	@Autowired
	private SystemImageService systemImageService;
	
	@Autowired
	private ImageTypeService imageTypeService;
	
	/***
	 * 上传文件
	 *
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "upload", method = RequestMethod.POST)
	@ResponseBody
	public RepBaseVO uploadFile(@RequestPart(value = "upfile") MultipartFile multipartFile,
			@RequestParam(value = "avatar_data", required = false) String avatarData,
			@RequestParam(value = "compress", defaultValue = "true") boolean compress) throws IOException {
		RepBaseVO repVO = new RepBaseVO<>();
		Map<String, Object> retMes = new HashMap<>(3);
		// 执行上传
		String fileName = FileUpload.fileUp(multipartFile, applicationConfig.getUploadFileLocation(),
				IdWorker.getId() + "");
		String filePath = applicationConfig.getUploadFileLocation() + fileName;
		// 如果含有裁剪数据则进行裁剪
		if (StringUtils.isNotBlank(avatarData)) {
			JSONObject object = JSON.parseObject(avatarData);
			int x = (int) Float.parseFloat(object.get("x").toString());
			int y = (int) Float.parseFloat(object.get("y").toString());
			int width = (int) Float.parseFloat(object.get("width").toString());
			int height = (int) Float.parseFloat(object.get("height").toString());
			ImgUtils.cut(new File(filePath), filePath, x, y, width, height);
		}
		// 判断图片大小，并进行适当的压缩
		File file = new File(filePath);
		Long fileSize = file.length();
		if (compress && fileSize > 600 * 1024) {
			double quailty = 1;
			if (fileSize < 1000 * 1024) {
				quailty = 0.8;
			} else if (fileSize < 2000 * 1024) {
				quailty = 0.7;
			} else {
				quailty = 0.65;
			}
			ImgUtils.compressFile1(file, quailty, filePath);
		}
		retMes.put("original", multipartFile.getOriginalFilename());
		retMes.put("url", applicationConfig.getFileServerPrefix() + fileName);
		retMes.put("title", multipartFile.getOriginalFilename());
		// retMes.put("state", "SUCCESS");
		repVO.setCommonSuccess().setData(retMes);
		return repVO;
	}
	
	/***
	 * 百度编辑器上传文件
	 *
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "ueditor/upload", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> ueditorUploadFile(@RequestParam(value = "upfile") MultipartFile multipartFile,
			@RequestParam(value = "avatar_data", required = false) String avatarData,
			@RequestParam(value = "compress", defaultValue = "true") boolean compress) throws IOException {
		Map<String, Object> retMes = new HashMap<>(4);
		// 执行上传
		String fileName = FileUpload.fileUp(multipartFile, applicationConfig.getUploadFileLocation(),
				IdWorker.getId() + "");
		String filePath = applicationConfig.getUploadFileLocation() + fileName;
		// 如果含有裁剪数据则进行裁剪
		if (StringUtils.isNotBlank(avatarData)) {
			JSONObject object = JSON.parseObject(avatarData);
			int x = (int) Float.parseFloat(object.get("x").toString());
			int y = (int) Float.parseFloat(object.get("y").toString());
			int width = (int) Float.parseFloat(object.get("width").toString());
			int height = (int) Float.parseFloat(object.get("height").toString());
			ImgUtils.cut(new File(filePath), filePath, x, y, width, height);
		}
		// 判断图片大小，并进行适当的压缩
		File file = new File(filePath);
		Long fileSize = file.length();
		if (compress && fileSize > 600 * 1024) {
			double quailty = 1;
			if (fileSize < 1000 * 1024) {
				quailty = 0.8;
			} else if (fileSize < 2000 * 1024) {
				quailty = 0.7;
			} else {
				quailty = 0.65;
			}
			ImgUtils.compressFile1(file, quailty, filePath);
		}
		retMes.put("original", multipartFile.getOriginalFilename());
		retMes.put("url", applicationConfig.getFileServerPrefix() + fileName);
		retMes.put("title", multipartFile.getOriginalFilename());
		retMes.put("state", "SUCCESS");
		return retMes;
	}
	
	@RequestMapping(value = "systemImage/upload", method = RequestMethod.POST)
	@ResponseBody
	public RepBaseVO systemImageUpload(@RequestPart(value = "upfile") MultipartFile multipartFile,
			@RequestParam(value = "avatar_data", required = false) String avatarData,
			@RequestParam(value = "compress", defaultValue = "true") boolean compress,
			@RequestParam(value = "type", required = true) Long type, String systemImageId) throws IOException {
		RepBaseVO repVO = new RepBaseVO<>();
		// 如为新增图片，判断数量是否超过上限
		if (StringUtils.isBlank(systemImageId)) {
			ImageType imageType = imageTypeService.selectByType(type);
			Integer haveUploadNum = systemImageService.countByType(type);
			if (imageType.getMax() != -1 && haveUploadNum >= imageType.getMax()) {
				return repVO.setCommonFail().setMes("上传数量超过上限，最多可上传" + imageType.getMax() + "张");
			}
		}
		
		Map<String, Object> retMes = new HashMap<>(4);
		// 执行上传
		String fileName = FileUpload.fileUp(multipartFile, applicationConfig.getUploadFileLocation(),
				IdWorker.getId() + "");
		String filePath = applicationConfig.getUploadFileLocation() + fileName;
		// 如果含有裁剪数据则进行裁剪
		if (StringUtils.isNotBlank(avatarData)) {
			JSONObject object = JSON.parseObject(avatarData);
			int x = (int) Float.parseFloat(object.get("x").toString());
			int y = (int) Float.parseFloat(object.get("y").toString());
			int width = (int) Float.parseFloat(object.get("width").toString());
			int height = (int) Float.parseFloat(object.get("height").toString());
			ImgUtils.cut(new File(filePath), filePath, x, y, width, height);
		}
		// 判断图片大小，并进行适当的压缩
		File file = new File(filePath);
		Long fileSize = file.length();
		if (compress && fileSize > 600 * 1024) {
			double quailty = 1;
			if (fileSize < 1000 * 1024) {
				quailty = 0.8;
			} else if (fileSize < 2000 * 1024) {
				quailty = 0.7;
			} else {
				quailty = 0.65;
			}
			ImgUtils.compressFile1(file, quailty, filePath);
		}
		retMes.put("original", multipartFile.getOriginalFilename());
		retMes.put("url", applicationConfig.getFileServerPrefix() + "/uploads/" + fileName);
		retMes.put("title", multipartFile.getOriginalFilename());
		// retMes.put("state", "SUCCESS");
		repVO.setCommonSuccess().setData(retMes);
		return repVO;
	}
	
}
